<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar van-nav-bar_title" title="登录">
      <div @click="$router.back()" class="btn" slot="left">
        <van-icon class="back" name="arrow-left"></van-icon>
        <span>返回</span>
      </div>
    </van-nav-bar>
    <!-- 表单区域 -->
    <van-form @submit="onSubmit" ref="loginForm">
      <van-field
        :rules="userFormRules.mobile"
        autofocus
        clearable
        maxlength="11"
        name="mobile"
        placeholder="请输入手机号"
        type="number"
        v-model="user.mobile"
      >
        <i class="toutiao toutiao-shouji" slot="left-icon"></i>
      </van-field>
      <van-field
        :rules="userFormRules.code"
        maxlength="6"
        name="code"
        placeholder="请输入验证码"
        type="number"
        v-model="user.code"
      >
        <template #button>
          <van-count-down
            :time="1000*60"
            @finish="isShowTime=false"
            format="ss s"
            v-if="isShowTime"
          />
          <van-button
            @click="onSendSms"
            class="send-sms-btn"
            native-type="button"
            round
            size="small"
            type="default"
            v-else
          >发送验证码</van-button>
        </template>
        <i class="toutiao toutiao-yanzhengma" slot="left-icon"></i>
      </van-field>
      <div style="margin: 16px;">
        <van-button block class="login-btn" native-type="submit" type="info">登录</van-button>
      </div>
    </van-form>
  </div>
</template>


<script>
import { login, sendSms } from '@/api/user'

export default {
  name: 'Login',
  components: {},
  props: {},
  data () {
    return {
      user: {
        mobile: '',
        code: ''
      },
      userFormRules: {
        mobile: [
          { required: true, message: '手机号不能为空' },
          { pattern: /^1[3|5|7|8|9]\d{9}$/, message: '手机号格式错误' }
        ],
        code: [
          { required: true, message: '验证码不能为空' },
          { pattern: /^\d{6}$/, message: '验证码格式错误' }
        ]
      },
      isShowTime: false
    }
  },
  computed: {},
  watch: {},
  created () {

  },
  mounted () { },
  methods: {
    async onSubmit () {
      // 获取表单数据
      const user = this.user
      //  表单验证

      // 在组间中必须通过this.$store 来使用
      this.$toast.loading({
        message: '登录中...',
        forbidClick: true,
        duration: 0

      });

      // 提交表单请求登录
      try {
        const { data: res } = await login(user)
        console.log('登录成功', res)
        this.$store.commit('setUser', res.data)
        this.$toast.success('登录成功')
        this.$router.back()
      } catch (err) {
        if (err.response.status === 400) {

          this.$toast.fail('手机号或验证码错误')
        } else {
          this.$toast.fail('登录失败，请重新输入！')
        }

      }

      // 根据请求的数据处理后期结果
    },
    async onSendSms () {
      // 校验手机号
      try {
        this.$refs.loginForm.validate('mobile')
        // console.log('验证成功')
      } catch (err) {
        console.log('验证失败', err)
        return
      }
      // 倒计时
      this.isShowTime = true
      // 发送请求接收验证码
      try {
        await sendSms(this.user.mobile)
        this.$toast('发送成功')
      } catch (err) {
        this.isShowTime = false
        if (err.response.status === 429) {
          this.$toast('发送的太频繁，请稍后在尝试！')
        } else {
          this.$toast('发送失败，请稍后重试')
        }
      }
    }

  }
}
</script>

<style lang="less" scoped>
.toutiao {
  font-size: 37px;
}
.send-sms-btn {
  background-color: #ededed;
  color: #999;
  font-weight: 700;
  height: 100%;
}
.login-btn {
  background-color: #6db4fb;
}
.btn {
  color: #fff;
  display: flex;
  align-items: center;
  .back {
    color: #fff;
  }
}
</style>